package com.tywj.buscustomerapp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.BaseReqeust;
import com.tywj.buscustomerapp.model.bean.MonthTBean;
import com.tywj.buscustomerapp.model.bean.MonthTicketBean;
import com.tywj.buscustomerapp.model.bean.MyTicketBean;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.SortComparator;
import com.tywj.buscustomerapp.utils.TaylorClickLinstener;
import com.tywj.buscustomerapp.utils.TimeRender;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.view.adapter.ViewPagerTicketAdapter;
import com.tywj.buscustomerapp.view.adapter.ViewPagerViewAdapter;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.tywj.buscustomerapp.view.myview.ToastJoe;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MyMonthTicketActivity extends BaseActivity {

    @BindView(R.id.viewpager) ViewPager viewPager;
    @BindView(R.id.position) TextView positionName;
    @BindView(R.id.error) RelativeLayout errorLayout;
    @BindView(R.id.head_title) TextView title;
    @BindView(R.id.head_left) ImageView back;
    private ViewPagerTicketAdapter adapter;
    private List<MonthTBean.DataBean> tickets;
    private LoadingDialog waitDialog;
    private AlertDialog build;


    @Override
    protected int getContentId() {
        return R.layout.activity_my_month_ticket;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        title.setText("我的月票");
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                positionName.setText((position+1)+"/"+tickets.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        errorLayout.setVisibility(View.VISIBLE);
        if (waitDialog==null)
            waitDialog = new LoadingDialog(MyMonthTicketActivity.this);
        if (!waitDialog.isShowing())
            waitDialog.show();
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        //创建一个请求
        String url = Config.SBURL + "/order/getMyMonthTicket";
        JSONObject object = new JSONObject();
        try {
            object.put("yhm", SPUtils.getUserPhone(getApplicationContext()));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.i("taylor1", String.valueOf(jsonObject));
                MonthTBean data = new Gson().fromJson(String.valueOf(jsonObject), MonthTBean.class);
                if (waitDialog!=null && waitDialog.isShowing())
                    waitDialog.dismiss();
                if (data != null && data.getData()!=null && data.getData().size()>0) {
                    tickets = data.getData();
                    if (positionName!=null)
                        positionName.setText(1 + "/" + tickets.size());
                    adapter = new ViewPagerTicketAdapter(MyMonthTicketActivity.this, tickets,MyMonthTicketActivity.this);
                    viewPager.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    errorLayout.setVisibility(View.GONE);
                } else {
                    if(tickets!=null){
                        tickets.clear();
                        adapter.notifyDataSetChanged();
                    }
                    if (waitDialog.isShowing())
                        waitDialog.dismiss();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                if(tickets!=null){
                    tickets.clear();
                    adapter.notifyDataSetChanged();
                }
                if (waitDialog.isShowing())
                    waitDialog.dismiss();
            }
        });
        //将创建的请求添加到队列中
        requestQueue.add(jsonObjectRequest);
    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(waitDialog!=null){
            waitDialog.dismiss();
        }
        if(build!=null && build.isShowing()){
            build.dismiss();
        }
    }



    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("我的月票");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("我的月票");
        MobclickAgent.onPause(this);
    }

}
